package blueoceanpay

import (
	"errors"
	"fmt"
	"thirdpaysdk/core"
	"thirdpaysdk/utils/signature"
	"time"
	//"github.com/astaxie/beego"
)

type RefundsResponse struct {
	ReturnCode      string `json:"return_code"`
	ReturnMsg       string `json:"return_msg,omitempty"`
	ResultCode      string `json:"result_code"`
	RefundId        string `json:"refund_id"`
	PartnerRefundId string `json:"partner_refund_id"`
	Amount          int    `json:"amount"`
	Currency        string `json:"currency"`
}

func Refunds(partnerCode, orderId, credentialCode, refundId string, fee int) (rsp *RefundsResponse, err error) {
	incompleteURL := "https://mpay.royalpay.com.au/api/v1.0/gateway/partners/%s/orders/%s/refunds/%s?time=%d&nonce_str=%s&sign=%s"

	rsp = &RefundsResponse{}
	request := struct {
		Fee int `json:"fee"`
	}{
		Fee: fee,
	}
	curTime := time.Now().Unix() * 1000
	nonceStr := signature.GetNonceStr(20)
	sign := signature.MakeSha256Sign(partnerCode, curTime, nonceStr, credentialCode)
	serverUrl := fmt.Sprintf(incompleteURL, partnerCode, orderId, refundId, curTime, nonceStr, sign)
	client := core.GetPayJSONClient("PUT", serverUrl)
	tbdata, e := client.PayJsonExcute(request, rsp)
	if e != nil {
		err = e
		return
	}
	if rsp.ReturnCode != "SUCCESS" {
		err = errors.New(string(tbdata))
		return
	}
	return
}
